import koaSession from "koa-session";
import Koa, { Context } from 'koa'
import { KoaMiddlewareInterface, Middleware } from "routing-controllers";
import { Service } from "typedi";

@Service()
@Middleware({ type: 'before' })
export class SessionMiddleware implements KoaMiddlewareInterface {
  async use(context: Context, next: (err?: any) => Promise<any>): Promise<any> {
    console.log('session middleware start');
    context.app.keys = ['some secret hurr'];
    context.app.use(koaSession({
      key: 'koa:sess1',
      maxAge: 1000 * 60 * 60,
      overwrite: true,
      httpOnly: true,
      signed: true,
      rolling: true,
    }, context.app));
    await next();
  }
}
